//
//  BottomSheet.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/17.
//  Copyright © 2020 一介布衣. All rights reserved.
//

#import "BottomSheet.h"
#import <STPopup/STPopup.h>
#import "SingleColumnDataPickerView.h"

static const NSInteger BottomsheetHeight = 248;
static const NSInteger BottomsheetBarHeight = 40;

@implementation BottomSheetBuilder

- (instancetype)init{
    if (self = [super init]) {
        _sheetHeight = BottomsheetHeight;
    }
    return self;
}
@end

@interface BottomSheet ()
//@property (nonatomic, strong) BottomSheetBuilder *builder;

@property (nonatomic, copy) BottomSheetConfirmBlock confirmBlock;
@property (nonatomic, copy) BottomSheetCancelBlock cancelBlock;

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation BottomSheet

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initPop];
    //[self setupViews];
}

- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat pickerViewHeight = self.builder.sheetHeight > BottomsheetHeight ? self.builder.sheetHeight : BottomsheetHeight;
    self.contentSizeInPopup = CGSizeMake(self.view.frame.size.width, pickerViewHeight);
    self.popupController.navigationBarHidden = YES;
    self.popupController.hidesCloseButton = YES;
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissView)]];
    
    [self setupViews];
}

- (void)setupViews{
    //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissView)];
    //[self.view addGestureRecognizer:tap];
    [self.view addSubview:self.containerView];
    //self.containerView.backgroundColor = [UIColor blueColor];
    
    if (self.builder.hideTitle == NO) {
        [self.containerView addSubview:self.confirmButton];
        [self.containerView addSubview:self.cancelButton];
        [self.containerView addSubview:self.titleLabel];
    }
}

+(BottomSheetBuilder*) sheetBuilder {
    return [[BottomSheetBuilder alloc]init];
}
/**
 * 添加contentView
 */
-(void) setBootSheetContentView:(UIView *) content {
    _contentView = content;
    [self.containerView addSubview:_contentView];
    
    if (self.builder.hideTitle == NO) {
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.containerView).offset(BottomsheetBarHeight);
            make.bottom.equalTo(self.containerView);
        }];
    }
}

-(void) setTitle:(NSString *) title withColor:(int) color {
    
}

-(void) setItemLeft:(NSString *) leftTitle right:(NSString *) rightTitle {
    
}

-(void) show:(UIViewController*) controller {
    STPopupController *popVericodeController = [[STPopupController alloc] initWithRootViewController:self];
    popVericodeController.style = STPopupStyleBottomSheet;
    [popVericodeController presentInViewController:controller];
}

-(void) show:(UIViewController*) controller confirm:(BottomSheetConfirmBlock) confirm cancel:(BottomSheetCancelBlock) cancel {
    self.confirmBlock = confirm;
    self.cancelBlock = cancel;
    [self show:controller];
}

#pragma mark - getter
- (UIView *)containerView{
    if (!_containerView) {
        CGFloat pickerViewHeight = self.builder.sheetHeight > BottomsheetHeight ? self.builder.sheetHeight : BottomsheetHeight;
        _containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, pickerViewHeight)];
        _containerView.backgroundColor = self.builder.pickerColor ?: UIColor.whiteColor;
    }
    return _containerView;
}

/**
 *
 */
- (UILabel *) titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 0, SCREEN_WIDTH - 130, BottomsheetBarHeight)];
        _titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_titleLabel setText:self.builder.titleText];
        UIColor *color = self.builder.titleTextColor ?: UIColor.darkTextColor;
        [_titleLabel setTextColor:color];
    }
    
    return _titleLabel;
}

/**
 *
 */
- (UIButton *)confirmButton {
    if (!_confirmButton) {
        _confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 54, 0, 40, BottomsheetBarHeight)];
        _confirmButton.backgroundColor = UIColor.clearColor;
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        NSString *title = self.builder.confirmText.length ? self.builder.confirmText : @"确定";
        [_confirmButton setTitle:title forState:UIControlStateNormal];
        UIColor *color = self.builder.confirmTextColor ?: UIColor.darkTextColor;
        [_confirmButton setTitleColor:color forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}


- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(16, 0, 40, BottomsheetBarHeight)];
        _cancelButton.backgroundColor = UIColor.clearColor;
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        NSString *title = self.builder.cancelText.length ? self.builder.cancelText : @"取消";
        [_cancelButton setTitle:title forState:UIControlStateNormal];
        UIColor *color = self.builder.cancelTextColor ?: UIColor.grayColor;
        [_cancelButton setTitleColor:color forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

#pragma mark - event response

- (void)confirm:(UIButton *)button{
    [self.popupController dismiss];
    if (self.confirmBlock) {
        self.confirmBlock();
    }
}

- (void)dissView{
    [self.popupController dismiss];
}

- (void)cancel:(UIButton *)button{
    [self dissView];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

@end
